<?php
declare (strict_types = 1);

namespace app\index\model\charge;

use think\facade\Db;
use think\Model;

/**
 * @mixin \think\Model
 */
class ChargePileGun extends Model
{
    protected $pk = "id";

    /**
     * 获取指定站点所有枪的统计信息
     * @param $stationId
     * @return mixed
     */
    function getStationGunCount($stationId){
        $where = ['p.station_id'=>$stationId,'p.is_delete'=>1,'p.is_status'=>1];
        //快充数量信息
        $backData['fast_gun_number'] = $this->table("jy_charge_pile_gun")->alias("g")
            ->leftJoin('jy_charge_pile p','g.pile_id = p.id')
            ->where($where)->where(["p.charge_type"=>1])->count();
        $backData['fast_free_gun_number'] = $this->table("jy_charge_pile_gun")->alias("g")
            ->leftJoin('jy_charge_pile p','g.pile_id = p.id')
            ->where($where)->where(["p.charge_type"=>1,'g.is_status'=>1])->count();
        //慢充数量信息
        $backData['slow_gun_number'] = $this->table("jy_charge_pile_gun")->alias("g")
            ->leftJoin('jy_charge_pile p','g.pile_id = p.id')
            ->where($where)->where(["p.charge_type"=>2])->count();
        $backData['slow_free_gun_number'] = $this->table("jy_charge_pile_gun")->alias("g")
            ->leftJoin('jy_charge_pile p','g.pile_id = p.id')
            ->where($where)->where(["p.charge_type"=>2,'g.is_status'=>1])->count();
        return $backData;
    }

    /**
     * 获取指定站点的充电桩
     * @param $stationId
     * @param $page
     * @param $limit
     * @return mixed
     */
    function getStationGunList($stationId,$page=1,$limit=15,$field = ''){
        $where = ['p.station_id'=>$stationId,'p.is_delete'=>1,'p.is_status'=>1];
        $statusList = [0=>'离线',1=>'空闲',2=>'异常',3=>'充电'];
        $deviceType = [1=>'直流',2=>'交流'];
        $chargeType = [1=>'快充',2=>'慢充'];
        //充电桩信息
        $count = $this->table("jy_charge_pile_gun")->alias("g")
            ->leftJoin('jy_charge_pile p','g.pile_id = p.id')
            ->where($where)->order('p.pile_sort','asc')->count();
        $sql = $this->table("jy_charge_pile_gun")->alias("g")
            ->leftJoin('jy_charge_pile p','g.pile_id = p.id')
            ->where($where)->order('p.pile_sort','asc')->page($page,$limit);
        if($field) $sql = $sql->field($field);
        $list =$sql->select()->each(function ($item)use($statusList,$deviceType,$chargeType){
                if($item['cost_status'] == 0){
                    $item['status_name'] = '充电桩暂未启用';
                }else{
                    $item['status_name'] = $statusList[$item['gun_status']];
                }
                $item['device_name'] = $deviceType[$item['device_type']];
                $item['charge_name'] = $chargeType[$item['charge_type']];
                $item['tip_cost'] = bcadd($item['charge_cost'],$item['service_cost'],2) ;
                $item['peak_cost'] = bcadd($item['peak_electric_cost'],$item['peak_service_cost'],2) ;
                $item['flat_cost'] = bcadd($item['flat_electric_cost'],$item['flat_service_cost'],2) ;
                $item['valley_cost'] = bcadd($item['valley_electric_cost'],$item['valley_service_cost'],2) ;
                return $item;
            })->toArray();
        return compact('count','list');
    }

    /**
     * 返回充电枪信息
     * @param $where
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getGunInfoByWhere($where,$field="*"){
        return $this->where($where)->find();
    }

    /**
     * 返回充电桩对应的状态
     * @return array
     */
    function backGunStatus(){
        return [
            'status_list'=>[0=>'离线',1=>'空闲',2=>'异常',3=>'充电'],
            'device_type'=>[1=>'直流',2=>'交流'],
            'charge_type'=>[1=>'快充',2=>'慢充'],
        ];
    }

    /**
     * 获取充电桩对应的充电枪
     * @param $pile
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getPileGun($pileId){
        $where = ['pile_id'=>$pileId];
        $gunList = $this->where($where)->select();
        if($gunList){
            $gunList = $gunList->toArray();
            $getStatus = $this->backGunStatus();
            foreach ($gunList as $key=>$value){
                $gunList[$key]['status_name'] = $getStatus['status_list'][$value['is_status']];
            }
        }else{
            $gunList =[];
        }
        return  $gunList;
    }

}
